#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-11-04 10:17 上午
# @Author  : yangdy
# @File    : 2020-11-04.57.插入区间
# @Software: PyCharmCE
from tools.my_methods import *


class Solution:
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
        if not intervals:
            if not newInterval:
                return []
            return [newInterval]
        intervals.append(newInterval)
        intervals.sort(key=lambda x: x[0])
        stack = [intervals[0]]
        for i in range(1, len(intervals)):
            ii = intervals[i]
            if ii[0] <= stack[-1][1]:
                if ii[1] > stack[-1][1]:
                    stack[-1][1] = ii[1]
            else:
                stack.append(ii)
        return stack
